<?php

namespace  app\service;


use app\model\PartnerLogModel;
use app\model\PartnerUserModel;
use app\model\WarehouseModel;
use app\utils\Md5Service;
use think\facade\Db;

Class WmsWarehouseService{

    public  static  function  add_admin($param,$logParam){
        $partnerInfo= PartnerUserModel::GetPartnerUserInfo(['mobile'=>$param['mobile']]);
        if (!empty($partnerInfo)){
            returnFail(21013,'信息错误');
        }

        $add['add_time']=getNowDateTime();
        $add['status']=1;
        $add['name']=$param['name'];
        $add['nickname']=$param['name'];
        $salt=GetRandStr(10);
        $add['password']=Md5Service::encryptMd5($param['password'],$salt);
        $add['salt']=$salt;
        $add['partner_id']=$param['partner_id'];
        $add['avatar']='https://img.xianlian.cc/upload/logo/logo.png';
        $add['wms_api_salt_h5']=GetRandStr(10);
        $add['wms_api_salt_app']=GetRandStr(10);
        $add['type']=2;
        $add['mobile']=$param['mobile'];
        $add['warehouse_id']=$param['warehouse_id'];
        return PartnerUserModel::addPartnerUserInfo($add,$logParam);

    }

    public static  function  del_admin($param,$logParam,$id){

        return PartnerUserModel::delPartnerUserInfo($param,$logParam,$id);
    }

    public  static  function  modify_admin($param,$logParam,$id): bool
    {
//        $partnerInfo= PartnerUserModel::GetPartnerUserInfo(['mobile'=>$param['mobile']]);
//        if (!empty($partnerInfo)){
//            returnFail(21013,'信息错误');
//        }

          $add['status']=1;
        $add['name']=$param['name'];
        $add['nickname']=$param['name'];
        $salt=GetRandStr(10);
        if($param['password']!=""){
            $add['password']=Md5Service::encryptMd5($param['password'],$salt);
            $add['salt']=$salt;
              $add['wms_api_salt_h5']=GetRandStr(10);
         $add['wms_api_salt_app']=GetRandStr(10);
        }
       
        $add['partner_id']=$param['partner_id'];
       
         $add['mobile']=$param['mobile'];
         return PartnerUserModel::modifyPartnerUserInfo($add,$logParam,$id);

    }
    public static  function info()
    {

        $warehouseType=WarehouseModel::GetAllWarehouseType();
        $bussineType=WarehouseModel::GetAllBussineType();
        return [
            'warehouseType'=>$warehouseType,
            'bussineType'=>$bussineType
        ];
    }

    public  static  function  admin_lists($param){
        return  PartnerUserModel::admin_lists($param);
    }

    public  static  function  lists($param){
         return  WarehouseModel::list($param);
    }

    public  static  function  line_data($param){
        return  WarehouseModel::line_data($param);
    }

    public  static  function  add($data,$logParam)
    {


        Db::startTrans();
       $res= WarehouseModel::add($data);
        $dataLog=[
            'partner_user_id'=>$logParam['uid'],
            'log_type'=>2,
            'request_ip'=>$logParam['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'添加仓库',
            'status'=>1,
            'partner_id'=>$data['partner_id']
        ];
        
        if ($res){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
    public  static  function  edit($data,$logParam,$id)
    {


        Db::startTrans();
        $data['update_time']=getNowDateTime();
         $res= WarehouseModel::edit($data,$id);
        $dataLog=[
            'partner_user_id'=>$logParam['uid'],
            'log_type'=>2,
            'request_ip'=>$logParam['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'修改仓库信息',
            'status'=>1,
            'partner_id'=>$data['partner_id']
        ];
        
          if ($res){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();

            return 10003;
        }
    }
    public  static  function  del($data,$logParam,$id)
    {


        Db::startTrans();
        $res= WarehouseModel::del($id);
        $dataLog=[
            'partner_user_id'=>$logParam['uid'],
            'log_type'=>2,
            'request_ip'=>$logParam['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'删除仓库信息',
            'status'=>1,
            'partner_id'=>$data['partner_id']
        ];
        
        if ($res){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
}